package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IUploadPhotoView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.DialogItemBean;
import com.cloudsigns.factorymanager2.engineering.customview.BottomListDialog;
import com.cloudsigns.factorymanager2.engineering.presenter.UploadPhotoPresenter;
import com.cloudsigns.factorymanager2.engineering.utils.PhotoUtil;
import com.cloudsigns.factorymanager2.project.bean.SurVeyOrderBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedBackForSizeBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class SurveyAddSizeFeedBackActivity1 extends Activity implements IUploadPhotoView, View.OnClickListener, AdapterView.OnItemClickListener, BottomListDialog.OnItemDialogClick {
    private final String TAG = SurveyAddSizeFeedBackActivity1.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView btn_upload;
    private EditText edt_survey_content;
    private View ll_normal_feedback, ll_special_feedback;
    private EditText edt_width, edt_height, edt_corner, edt_hightDif;
    private EditText edt_specialWidth, edt_specialHight, edt_arcLength, edt_arcHight;
    private GridView  gridView_special;
    private GridAdapter  gridViewSpecialAdapter;
    private List<String> specialPathList = new ArrayList<>();
    private List<String> serverNormalUrlList = new ArrayList<>();
    private List<String> serverSpecialUrlList = new ArrayList<>();
    private String filePath;
    private final int NORMAL_FEEDBACK = 1;
    private final int UNSUAL_FEEDBACK = 2;
    private int feedBackType = NORMAL_FEEDBACK;
    private SurVeyOrderBean surVeyOrderBean;
    private UploadPhotoPresenter uploadPhotoPresenter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_survey_add_size_feedback1);

        uploadPhotoPresenter = new UploadPhotoPresenter(this);
        init();
        tv_title.setText("尺寸记录");

        if (null != getIntent()) {
            surVeyOrderBean = (SurVeyOrderBean) getIntent().getSerializableExtra(ConstantUtil.KEY_1);
        }

    }

    private void init() {
        rl_back = (RelativeLayout) findViewById(R.id.rl_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        btn_upload = (TextView) findViewById(R.id.btn_upload);
        ll_normal_feedback = findViewById(R.id.ll_normal_feedback);
        ll_special_feedback = findViewById(R.id.ll_special_feedback);
        edt_width = findViewById(R.id.edt_width);
        edt_height = findViewById(R.id.edt_height);
        edt_corner = findViewById(R.id.edt_corner);
        edt_hightDif = findViewById(R.id.edt_hightDif);
        edt_specialWidth = findViewById(R.id.edt_specialWidth);
        edt_specialHight = findViewById(R.id.edt_specialHight);
        edt_arcLength = findViewById(R.id.edt_arcLength);
        edt_arcHight = findViewById(R.id.edt_arcHight);
        edt_survey_content = findViewById(R.id.edt_survey_content);
        gridView_special = findViewById(R.id.gridView_special);

        specialPathList.add("paizhao");

        gridView_special.setNumColumns(3);
        gridViewSpecialAdapter = new GridAdapter(this);
        gridViewSpecialAdapter.setData(specialPathList);
        gridView_special.setAdapter(gridViewSpecialAdapter);
        gridView_special.setOnItemClickListener(this);

        rl_back.setOnClickListener(this);
        btn_upload.setOnClickListener(this);




    }

    private void showListDialog() {
        DialogItemBean[] items = new DialogItemBean[]{
                new DialogItemBean(R.string.camera,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.gallary,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.cancel,
                        R.layout.dialog_list_item_normal)};
        BottomListDialog dialog = new BottomListDialog(this, Arrays.asList(items));
        dialog.setItemClick(this);
        dialog.show();
    }

    private void setFeedBackType(){
        if (!TextUtils.isEmpty(edt_width.getText().toString())
                &&!TextUtils.isEmpty(edt_height.getText().toString())
                &&!TextUtils.isEmpty(edt_corner.getText().toString())
                &&!TextUtils.isEmpty(edt_hightDif.getText().toString())
                &&TextUtils.isEmpty(edt_specialWidth.getText().toString())
                ||TextUtils.isEmpty(edt_specialHight.getText().toString())
                ||TextUtils.isEmpty(edt_arcLength.getText().toString())
                ||TextUtils.isEmpty(edt_arcHight.getText().toString())){

            feedBackType = NORMAL_FEEDBACK;
            Log.e(TAG,"--------------1");
        }


        if (!TextUtils.isEmpty(edt_specialWidth.getText().toString())
                &&!TextUtils.isEmpty(edt_specialHight.getText().toString())
                &&!TextUtils.isEmpty(edt_arcLength.getText().toString())
                &&!TextUtils.isEmpty(edt_arcHight.getText().toString())
                &&TextUtils.isEmpty(edt_width.getText().toString())
                ||TextUtils.isEmpty(edt_height.getText().toString())
                ||TextUtils.isEmpty(edt_corner.getText().toString())
                ||TextUtils.isEmpty(edt_hightDif.getText().toString())){

            feedBackType = UNSUAL_FEEDBACK;
            Log.e(TAG,"--------------2");
        }
    }


    private void submitFeedBack() {
        setFeedBackType();
        if (feedBackType == NORMAL_FEEDBACK) {
            if (TextUtils.isEmpty(edt_survey_content.getText().toString())){
                Toast.makeText(this,"请输入勘察内容",Toast.LENGTH_SHORT).show();
                return;
            } else if (TextUtils.isEmpty(edt_width.getText().toString())){
                Toast.makeText(this,"请输入宽度",Toast.LENGTH_SHORT).show();
                return;
            }else if (TextUtils.isEmpty(edt_height.getText().toString())){
                Toast.makeText(this,"请输入高度",Toast.LENGTH_SHORT).show();
                return;
            }else if (TextUtils.isEmpty(edt_corner.getText().toString())){
                Toast.makeText(this,"请输入转角",Toast.LENGTH_SHORT).show();
                return;
            }else if (TextUtils.isEmpty(edt_hightDif.getText().toString())){
                Toast.makeText(this,"请输入高差",Toast.LENGTH_SHORT).show();
                return;
            }else {
                SurveyFeedBackForSizeBean surveyFeedBackForSizeBean = new SurveyFeedBackForSizeBean();
                surveyFeedBackForSizeBean.setContent(edt_survey_content.getText().toString().trim());

                String widthStr = edt_width.getText().toString().trim();
                if (!TextUtils.isEmpty(widthStr))
                    surveyFeedBackForSizeBean.setWidth(Integer.parseInt(widthStr));

                String heightStr = edt_height.getText().toString().trim();
                if (!TextUtils.isEmpty(heightStr))
                    surveyFeedBackForSizeBean.setHight(Integer.parseInt(heightStr));

                String cornertr = edt_corner.getText().toString().trim();
                if (!TextUtils.isEmpty(cornertr))
                    surveyFeedBackForSizeBean.setCorner(Integer.parseInt(cornertr));

                String hightDifStr = edt_hightDif.getText().toString().trim();
                if (!TextUtils.isEmpty(hightDifStr))
                    surveyFeedBackForSizeBean.setHightDif(Integer.parseInt(hightDifStr));

                surveyFeedBackForSizeBean.setImgUrlList(serverNormalUrlList);
                surveyFeedBackForSizeBean.setSurveySizeType(CommonEnum.SurveySizeType.normal);

                Toast.makeText(this, "上传成功！", Toast.LENGTH_SHORT).show();
                Intent mIntent = new Intent();
                mIntent.putExtra(ConstantUtil.KEY_1, surveyFeedBackForSizeBean);
                setResult(RESULT_OK, mIntent);
                finish();
            }
        } else if (feedBackType == UNSUAL_FEEDBACK) {
            if (TextUtils.isEmpty(edt_survey_content.getText().toString())){
                Toast.makeText(this,"请输入勘察内容",Toast.LENGTH_SHORT).show();
                return;
            } else if (TextUtils.isEmpty(edt_specialWidth.getText().toString())){
                Toast.makeText(this,"请输入宽度",Toast.LENGTH_SHORT).show();
                return;
            }else if (TextUtils.isEmpty(edt_specialHight.getText().toString())){
                Toast.makeText(this,"请输入高度",Toast.LENGTH_SHORT).show();
                return;
            }else if (TextUtils.isEmpty(edt_arcLength.getText().toString())){
                Toast.makeText(this,"请输入弧长",Toast.LENGTH_SHORT).show();
                return;
            }else if (TextUtils.isEmpty(edt_arcHight.getText().toString())){
                Toast.makeText(this,"请输入弧高",Toast.LENGTH_SHORT).show();
                return;
            }else {
                SurveyFeedBackForSizeBean surveyFeedBackForSizeBean = new SurveyFeedBackForSizeBean();
                surveyFeedBackForSizeBean.setContent(edt_survey_content.getText().toString().trim());

                String specialWidthStr = edt_specialWidth.getText().toString().trim();
                if (!TextUtils.isEmpty(specialWidthStr))
                    surveyFeedBackForSizeBean.setSpecialWidth(Integer.parseInt(specialWidthStr));

                String specialHightStr = edt_specialHight.getText().toString().trim();
                if (!TextUtils.isEmpty(specialHightStr))
                    surveyFeedBackForSizeBean.setSpecialHight(Integer.parseInt(specialHightStr));

                String arcLengthStr = edt_arcLength.getText().toString().trim();
                if (!TextUtils.isEmpty(arcLengthStr))
                    surveyFeedBackForSizeBean.setArcLength(Integer.parseInt(arcLengthStr));

                String arcHightStr = edt_arcHight.getText().toString().trim();
                if (!TextUtils.isEmpty(arcHightStr))
                    surveyFeedBackForSizeBean.setArcHight(Integer.parseInt(arcHightStr));

                surveyFeedBackForSizeBean.setImgUrlList(serverSpecialUrlList);
                surveyFeedBackForSizeBean.setSurveySizeType(CommonEnum.SurveySizeType.special);

                Toast.makeText(this, "上传成功！", Toast.LENGTH_SHORT).show();
                Intent mIntent = new Intent();
                mIntent.putExtra(ConstantUtil.KEY_1, surveyFeedBackForSizeBean);
                setResult(RESULT_OK, mIntent);
                finish();
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        String imgs = (String) adapterView.getItemAtPosition(i);
        if ("paizhao".equals(imgs)) {
            showListDialog();
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_upload:
                submitFeedBack();
                break;
            case R.id.rl_back:
                finish();
                break;

        }
    }

    @Override
    public void onItemDialogClick(DialogItemBean item) {
        switch (item.getmTextId()) {
            case R.string.camera: {
                PhotoUtil.startCamear(SurveyAddSizeFeedBackActivity1.this, null);
                break;
            }
            case R.string.gallary: {
                PhotoUtil.startGallery(SurveyAddSizeFeedBackActivity1.this, null);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {

            case PhotoUtil.CAMERA:
                if (resultCode == RESULT_OK) {
                    File picture = new File(PhotoUtil.filePath);
                    if (!picture.exists()) {
                        picture = new File(PhotoUtil.filePath);
                    }

                    filePath = PhotoUtil.CAMERA_SEVER + picture.getName();
                    filePath = PhotoUtil.imageCompressionAndSave(picture.getAbsolutePath(), filePath);
                    //override(100, 100)
                    if (feedBackType == NORMAL_FEEDBACK) {
                        if (specialPathList.contains("paizhao")) {
                            specialPathList.remove("paizhao");
                        }
                        if (specialPathList.size() < 3) {
                            specialPathList.add(filePath);
                            if (specialPathList.size() < 3)
                                specialPathList.add("paizhao");

                            gridViewSpecialAdapter.setData(specialPathList);
                            gridViewSpecialAdapter.notifyDataSetChanged();
                        }
                    } else if (feedBackType == UNSUAL_FEEDBACK) {
                        if (specialPathList.contains("paizhao")) {
                            specialPathList.remove("paizhao");
                        }
                        if (specialPathList.size() < 3) {
                            specialPathList.add(filePath);
                            if (specialPathList.size() < 3)
                                specialPathList.add("paizhao");

                            gridViewSpecialAdapter.setData(specialPathList);
                            gridViewSpecialAdapter.notifyDataSetChanged();
                        }
                    }
                    uploadPhotoPresenter.uploadPhtoto();
                }

                break;
            case PhotoUtil.GALLERY:
                if (resultCode == RESULT_OK) {

                    String path = FileUtil.getRealPathFromURI(this, Uri.parse(data.getData() + ""));

                    if (feedBackType == NORMAL_FEEDBACK) {
                        if (specialPathList.contains("paizhao")) {
                            specialPathList.remove("paizhao");
                        }
                        if (specialPathList.size() < 3) {
                            specialPathList.add(path);
                            if (specialPathList.size() < 3)
                                specialPathList.add("paizhao");

                            gridViewSpecialAdapter.setData(specialPathList);
                            gridViewSpecialAdapter.notifyDataSetChanged();
                        }
                    } else if (feedBackType == UNSUAL_FEEDBACK) {
                        if (specialPathList.contains("paizhao")) {
                            specialPathList.remove("paizhao");
                        }
                        if (specialPathList.size() < 3) {
                            specialPathList.add(path);
                            if (specialPathList.size() < 3)
                                specialPathList.add("paizhao");

                            gridViewSpecialAdapter.setData(specialPathList);
                            gridViewSpecialAdapter.notifyDataSetChanged();
                        }
                    }
                    File f = new File(path);
                    filePath = PhotoUtil.CAMERA_SEVER + f.getName();
                    filePath = PhotoUtil.imageCompressionAndSave(path, filePath);
                    uploadPhotoPresenter.uploadPhtoto();
                }
                break;
            case PhotoUtil.PHOTORESOULT:
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public String getFilePath() {
        return filePath;
    }


    @Override
    public void upLoadPhotoSuccessed(String uri) {
        if (feedBackType == NORMAL_FEEDBACK) {
            if (serverNormalUrlList.size() < 3)
                serverNormalUrlList.add(uri);
        } else if (feedBackType == UNSUAL_FEEDBACK) {
            if (serverSpecialUrlList.size() < 3)
                serverSpecialUrlList.add(uri);
        }

    }

    @Override
    public void upLoadPhotoFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
